package com.live.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.live.adapter.LiveInfoFriendSA;
import com.live.adapter.LiveInfoMsgSA;
import com.live.adapter.LiveInfoReplaySA;
import com.live.common.Constant;
import com.live.common.LiveCommon;
import com.live.task.LiveFriend;
import com.live.task.LiveInfoReplay;
import com.live.task.LiveMessage;
import com.live.task.LiveTopic;
import com.live.tools.LiveTools;

public class LiveInfoCenterActivity extends LiveBaseActivity {

	private LiveInfoReplaySA infoReplaySA;
	private LiveInfoMsgSA infoMsgSA;
	private LiveInfoFriendSA infoFriendSA;

	private ListView infoReplayLV;
	private ListView infoMsgLV;
	private ListView infoFriendLV;

	private List<Map<String, Object>> liveInfoReplayList = new ArrayList<Map<String, Object>>();
	private List<Map<String, Object>> liveInfoMsgList = new ArrayList<Map<String, Object>>();
	private List<Map<String, Object>> liveFriendList = new ArrayList<Map<String, Object>>();

	private LinearLayout liveInfoCenter;
	private LinearLayout infoReplayArea;
	private LinearLayout infoMsgArea;
	private LinearLayout infoFriendArea;
	private LinearLayout infoTabArea;

	private View liveFoot;
	private int topicPage = 0;
	private int topicIndex = 0;
	private int msgPage = 0;
	private int msgIndex = 0;
	private int friendPage = 0;
	private int friendIndex = 0;

	private int listIndex = 0;

	private Bundle bundle;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.live_info_center);

		/* 获取Intent中的Bundle对象 */
		bundle = this.getIntent().getExtras();
		bundle.putString("uId", store.getString("id", null));
		bundle.putString("userId", store.getString("userId", null));

		// 设置头部信息
		LiveCommon.setHead(context, (LinearLayout) findViewById(R.id.liveHead), R.string.title_info_center,
				R.string.reload_btn);

		// 设置头部按钮（刷新）
		Button btn = (Button) findViewById(R.id.headFuncBtn);
		btn.setVisibility(Constant.INVISIBLE);

		// 设置提醒数量
		setInfoCount(Constant.N);

		// 取得选项卡VIEW
		infoTabArea = (LinearLayout) findViewById(R.id.infoTabArea);

		// 取得回复提醒VIEW
		infoReplayArea = (LinearLayout) findViewById(R.id.infoReplayArea);
		infoReplayLV = (ListView) findViewById(R.id.infoReplayList);

		// 取得留言提醒VIEW
		infoMsgArea = (LinearLayout) findViewById(R.id.infoMsgArea);
		infoMsgLV = (ListView) findViewById(R.id.infoMsgList);

		// 取得好友提醒VIEW
		infoFriendArea = (LinearLayout) findViewById(R.id.infoFriendArea);
		infoFriendLV = (ListView) findViewById(R.id.infoFriendList);

		// 添加“读取更多按钮”
		liveFoot = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.live_foot, null, false);

		// 初始化-删除全部VIEW
		liveInfoCenter = (LinearLayout) findViewById(R.id.liveInfoCenter);
		liveInfoCenter.removeAllViews();

		// 设置回复提醒
		infoReplaySA = new LiveInfoReplaySA(context, liveInfoReplayList, R.layout.info_replay_list, new String[] {},
				new int[] {}, store.getString("userId", null));
		infoReplayLV.addFooterView(liveFoot);
		infoReplayLV.setAdapter(infoReplaySA);
		infoReplayLV.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {
				final Map<String, Object> obj = liveInfoReplayList.get(arg2);
				new AlertDialog.Builder(context).setItems(new String[] { "回复主题", "标记已读" },
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int index) {
								Bundle bundle = new Bundle();
								switch (index) {
								case 0:
									// 记录当前操作行
									listIndex = arg2;

									Intent intent = new Intent();
									intent.setClass(context, LiveReplayActivity.class);

									bundle.putString("id", (String) obj.get("id"));
									bundle.putString("replayId", (String) obj.get("replayId"));
									bundle.putString("matchId", (String) obj.get("matchId"));
									bundle.putString("userName", (String) obj.get("userId"));
									bundle.putString("atUserId", (String) obj.get("uId"));
									bundle.putString("userId", store.getString("id", null));
									intent.putExtras(bundle);
									startActivityForResult(intent, Constant.REQUEST_CODE);

									break;
								case 1:
									bundle.putString("id", (String) obj.get("id"));
									bundle.putInt("index", arg2);
									LiveTopic task = new LiveTopic(context, liveInfoReplayList, infoReplaySA,
											infoTabArea, Constant.ACTION_UPDATE, null, null);
									task.execute(bundle);
								}
							}
						}).show();
			}
		});
		infoReplayLV.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && topicIndex == infoReplaySA.getCount()) {
					topicPage = topicPage + 1;
					bundle.putInt("page", topicPage);
					LiveInfoReplay task = new LiveInfoReplay(context, liveInfoReplayList, infoReplaySA, infoTabArea,
							infoReplayLV, liveFoot);
					task.execute(bundle);
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

				// 计算最后可见条目的索引
				topicIndex = firstVisibleItem + visibleItemCount - 1;
			}
		});

		// 回复提醒
		TextView infoReplay = (TextView) findViewById(R.id.infoReplay);
		infoReplay.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				changeTabInit(R.id.infoReplayLine);

				liveInfoCenter.addView(infoReplayArea);

				bundle.putInt("page", 0);
				LiveInfoReplay task = new LiveInfoReplay(context, liveInfoReplayList, infoReplaySA, infoTabArea,
						infoReplayLV, liveFoot);
				task.execute(bundle);
			}
		});

		// 留言提醒
		infoMsgSA = new LiveInfoMsgSA(context, liveInfoMsgList, R.layout.info_msg_list, new String[] {}, new int[] {},
				store.getString("userId", null));
		infoMsgLV.addFooterView(liveFoot);
		infoMsgLV.setAdapter(infoMsgSA);
		infoMsgLV.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {
				final Map<String, Object> obj = liveInfoMsgList.get(arg2);
				new AlertDialog.Builder(context).setItems(new String[] { "回复留言", "标记已读" },
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int index) {
								Bundle bundle = new Bundle();
								switch (index) {
								case 0:
									// 记录当前操作行
									listIndex = arg2;

									Intent intent = new Intent();
									intent.setClass(context, LiveMessageActivity.class);

									bundle.putString("id", (String) obj.get("id"));
									bundle.putString("uId", store.getString("id", null));
									bundle.putString("fId", (String) obj.get("uId"));
									intent.putExtras(bundle);
									startActivityForResult(intent, Constant.REQUEST_CODE);

									break;
								case 1:
									bundle.putString("id", (String) obj.get("id"));
									bundle.putInt("index", arg2);
									LiveMessage task = new LiveMessage(context, liveInfoMsgList, infoMsgSA,
											infoTabArea, Constant.ACTION_UPDATE, null, null);
									task.execute(bundle);
								}
							}
						}).show();
			}
		});
		infoMsgLV.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && msgIndex == infoMsgSA.getCount()) {
					msgPage = msgPage + 1;
					bundle.putInt("page", msgPage);
					LiveMessage task = new LiveMessage(context, liveInfoMsgList, infoMsgSA, infoTabArea,
							Constant.ACTION_SELECT, infoMsgLV, liveFoot);
					task.execute(bundle);
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

				// 计算最后可见条目的索引
				msgIndex = firstVisibleItem + visibleItemCount - 1;
			}
		});
		TextView infoMsg = (TextView) findViewById(R.id.infoMsg);
		infoMsg.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				changeTabInit(R.id.infoMsgLine);

				liveInfoCenter.addView(infoMsgArea);

				bundle.putInt("page", 0);
				LiveMessage task = new LiveMessage(context, liveInfoMsgList, infoMsgSA, infoTabArea,
						Constant.ACTION_SELECT, infoMsgLV, liveFoot);
				task.execute(bundle);
			}
		});

		// 好友提醒
		infoFriendSA = new LiveInfoFriendSA(context, liveFriendList, R.layout.info_friend_list, new String[] {},
				new int[] {}, infoTabArea);
		infoFriendLV.addFooterView(liveFoot);
		infoFriendLV.setAdapter(infoFriendSA);
		TextView infoFriend = (TextView) findViewById(R.id.infoFriend);
		infoFriend.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				changeTabInit(R.id.infoFriendLine);

				liveInfoCenter.addView(infoFriendArea);

				String url = Constant.SITE_URL + "live_friend.php?locale=" + LiveTools.getLocaleLanguage() + "&";
				url += "uId=" + store.getString("id", null) + "&action=" + Constant.ACTION_SELECT + "&fState="
						+ Constant.ACTION_APPLY;
				Bundle bundle = new Bundle();
				bundle.putString("action", Constant.ACTION_SELECT);
				LiveFriend task = new LiveFriend(context, url, liveFriendList, infoFriendSA, null, infoTabArea,
						infoFriendLV, liveFoot);
				task.execute(bundle);
			}
		});
		infoFriendLV.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && friendIndex == infoFriendSA.getCount()) {
					friendPage = friendPage + 1;
					bundle.putInt("page", friendPage);
					String url = Constant.SITE_URL + "live_friend.php?locale=" + LiveTools.getLocaleLanguage() + "&";
					url += "uId=" + store.getString("id", null) + "&action=" + Constant.ACTION_SELECT + "&fState="
							+ Constant.ACTION_APPLY;
					bundle.putString("action", Constant.ACTION_SELECT);
					LiveFriend task = new LiveFriend(context, url, liveFriendList, infoFriendSA, null, infoTabArea,
							infoFriendLV, liveFoot);
					task.execute(bundle);
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

				// 计算最后可见条目的索引
				friendIndex = firstVisibleItem + visibleItemCount - 1;
			}
		});

		// 初始化
		bundle.putInt("page", 0);
		if (bundle.getString("type").startsWith(Constant.MESSAGE_TYPE_R)) {
			// 初始化默认回复提醒
			changeTabInit(R.id.infoReplayLine);
			liveInfoCenter.addView(infoReplayArea);
			LiveInfoReplay task = new LiveInfoReplay(context, liveInfoReplayList, infoReplaySA, infoTabArea,
					infoReplayLV, liveFoot);
			task.execute(bundle);
		} else if (bundle.getString("type").startsWith(Constant.MESSAGE_TYPE_M)) {
			// 初始化默认留言提醒
			changeTabInit(R.id.infoMsgLine);
			liveInfoCenter.addView(infoMsgArea);
			LiveMessage task = new LiveMessage(context, liveInfoMsgList, infoMsgSA, infoTabArea,
					Constant.ACTION_SELECT, infoMsgLV, liveFoot);
			task.execute(bundle);
		} else {
			// 初始化默认好友提醒
			changeTabInit(R.id.infoFriendLine);
			liveInfoCenter.addView(infoFriendArea);
			String url = Constant.SITE_URL + "live_friend.php?locale=" + LiveTools.getLocaleLanguage() + "&";
			url += "uId=" + store.getString("id", null) + "&action=" + Constant.ACTION_SELECT + "&fState="
					+ Constant.ACTION_APPLY;
			bundle.putString("action", Constant.ACTION_SELECT);
			LiveFriend task = new LiveFriend(context, url, liveFriendList, infoFriendSA, null, infoTabArea, infoFriendLV,
					liveFoot);
			task.execute(bundle);
		}
	}

	private void changeTabInit(int tab) {
		// 隐藏下划线
		TextView infoReplayLine = (TextView) findViewById(R.id.infoReplayLine);
		infoReplayLine.setVisibility(Constant.INVISIBLE);
		TextView infoPageLine = (TextView) findViewById(R.id.infoMsgLine);
		infoPageLine.setVisibility(Constant.INVISIBLE);
		TextView infoFriendLine = (TextView) findViewById(R.id.infoFriendLine);
		infoFriendLine.setVisibility(Constant.INVISIBLE);

		TextView view = (TextView) findViewById(tab);
		view.setVisibility(Constant.VISIBLE);

		liveInfoCenter.removeAllViews();
	}

	private void setInfoCount(String reset) {
		TextView infoReplayCount = (TextView) findViewById(R.id.infoReplayCount);
		ImageView infoReplayCountImg = (ImageView) findViewById(R.id.infoReplayCountImg);

		TextView infoMsgCount = (TextView) findViewById(R.id.infoMsgCount);
		ImageView infoMsgCountImg = (ImageView) findViewById(R.id.infoMsgCountImg);

		TextView infoFriendCount = (TextView) findViewById(R.id.infoFriendCount);
		ImageView infoFriendCountImg = (ImageView) findViewById(R.id.infoFriendCountImg);

		// 设置提醒数量
		String rCount = null;
		String mCount = null;
		String fCount = null;

		if (Constant.Y.equals(reset)) {
			rCount = String.valueOf(liveInfoReplayList.size());
			mCount = String.valueOf(liveInfoMsgList.size());
			fCount = String.valueOf(liveFriendList.size());
		} else {
			rCount = bundle.getString("rCount");
			mCount = bundle.getString("mCount");
			fCount = bundle.getString("fCount");
		}

		if (Constant.NUM_ZERO.equals(rCount)) {
			infoReplayCount.setVisibility(Constant.INVISIBLE);
			infoReplayCountImg.setVisibility(Constant.INVISIBLE);
		} else {
			infoReplayCount.setText(rCount);
			infoReplayCount.setVisibility(Constant.VISIBLE);
			infoReplayCountImg.setVisibility(Constant.VISIBLE);
		}
		if (Constant.NUM_ZERO.equals(mCount)) {
			infoMsgCount.setVisibility(Constant.INVISIBLE);
			infoMsgCountImg.setVisibility(Constant.INVISIBLE);
		} else {
			infoMsgCount.setText(mCount);
			infoMsgCount.setVisibility(Constant.VISIBLE);
			infoMsgCountImg.setVisibility(Constant.VISIBLE);
		}
		if (Constant.NUM_ZERO.equals(fCount)) {
			infoFriendCount.setVisibility(Constant.INVISIBLE);
			infoFriendCountImg.setVisibility(Constant.INVISIBLE);
		} else {
			infoFriendCount.setText(fCount);
			infoFriendCount.setVisibility(Constant.VISIBLE);
			infoFriendCountImg.setVisibility(Constant.VISIBLE);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == Constant.REQUEST_CODE) {
			if (resultCode != RESULT_CANCELED) {
				// 对于已读的数据，在列表中清除，并更新未读件数统计
				if (Constant.RELOAD.equals(data.getAction())) {
					if (findViewById(R.id.infoReplayArea) != null) {
						liveInfoReplayList.remove(listIndex);

						TextView infoReplayCount = (TextView) findViewById(R.id.infoReplayCount);
						ImageView infoReplayCountImg = (ImageView) findViewById(R.id.infoReplayCountImg);

						if (liveInfoReplayList.size() == 0) {
							infoReplayCount.setVisibility(Constant.INVISIBLE);
							infoReplayCountImg.setVisibility(Constant.INVISIBLE);
						} else {
							infoReplayCount.setText(String.valueOf(liveInfoReplayList.size()));
							infoReplayCount.setVisibility(Constant.VISIBLE);
							infoReplayCountImg.setVisibility(Constant.VISIBLE);
						}

						infoReplaySA.notifyDataSetChanged();
					} else if (findViewById(R.id.infoMsgArea) != null) {
						liveInfoMsgList.remove(listIndex);

						TextView infoMsgCount = (TextView) findViewById(R.id.infoMsgCount);
						ImageView infoMsgCountImg = (ImageView) findViewById(R.id.infoMsgCountImg);

						if (liveInfoMsgList.size() == 0) {
							infoMsgCount.setVisibility(Constant.INVISIBLE);
							infoMsgCountImg.setVisibility(Constant.INVISIBLE);
						} else {
							infoMsgCount.setText(String.valueOf(liveInfoMsgList.size()));
							infoMsgCount.setVisibility(Constant.VISIBLE);
							infoMsgCountImg.setVisibility(Constant.VISIBLE);
						}
						infoMsgSA.notifyDataSetChanged();
					}
				}
			}
		}
	}
}